package jnpf.message.service;

import cn.hutool.core.util.ObjectUtil;
import jnpf.base.UserInfo;
import jnpf.message.entity.SynThirdInfoEntity;
import jnpf.message.model.message.OpenBaseModel;
import jnpf.message.model.message.OpenDeptModel;
import jnpf.message.util.SynThirdConsts;
import jnpf.util.DateUtil;
import jnpf.util.UserProvider;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public interface ThirdOpenService {

	Object beforeProcess(OpenBaseModel openBaseModel, Map<String, OpenDeptModel> openDeptModelMap);

	void afterProcess(List<Object> result);

	/**
	 * 将组织、用户的信息写入同步表
	 * @param dataType              数据类型
	 * @param thirdObjId            第三方对象ID
	 * @param synState              同步状态(0:未同步;1:同步成功;2:同步失败)
	 * @param description
	 */
	default void saveSynThirdInfoEntity(OpenBaseModel.OperationType operationType,
	                                    String dataType, String thirdObjId, Integer synState,
	                                    String description, SynThirdInfoService synThirdInfoService, UserProvider userProvider) {
		SynThirdInfoEntity entity = synThirdInfoService.getInfoByThirdObjId(SynThirdConsts.THIRD_TYPE_OPEN, dataType, thirdObjId);
		if (Objects.isNull(entity)) {
			entity = new SynThirdInfoEntity();
		}
		UserInfo userInfo = userProvider.get();
		entity.setThirdType(Integer.valueOf(SynThirdConsts.THIRD_TYPE_OPEN));
		entity.setOperationType(ObjectUtil.isNull(operationType) ? 0 : operationType.getCode());
		entity.setDataType(Integer.valueOf(dataType));
		entity.setSysObjId(thirdObjId);
		entity.setThirdObjId(thirdObjId);
		entity.setEnabledMark(synState);
		// 备注当作同步失败信息来用
		entity.setDescription(description);
		entity.setCreatorUserId(userInfo.getUserId());
		entity.setCreatorTime(DateUtil.getNowDate());
		entity.setLastModifyUserId(userInfo.getUserId());
		// 修改时间当作最后同步时间来用
		entity.setLastModifyTime(DateUtil.getNowDate());
		synThirdInfoService.saveOrUpdate(entity);
	}
}
